import { createSlice, PayloadAction } from '@reduxjs/toolkit';
import { Song } from '@/faker/songs/ranking-songs';
export enum PLAY_MODE {
    LOOP, //列表循环
    RANDOM, //随机播放
    ONE //单曲循环
}
interface PlayerState {
    currentSong: Song | null;
    playList: Song[];
    isPlaying: boolean;
    currentTime: number;
    playMode: PLAY_MODE
}
export const defaultSong:Song = {
    id: 0,
    name: '酷我音乐',
    artist: '',
    picUrl: 'http://kwimg2.kuwo.cn/star/upload/37/25/1633954819501_.png',
    songUrl: '',
    album:{
        id:0,
        name:"",
        picUrl:"http://kwimg2.kuwo.cn/star/upload/37/25/1633954819501_.png",
        bio:"",
        songs:[]
    },
    duration:0,
}
const initialState: PlayerState = {
    currentSong: null,
    playList: [],
    isPlaying: false,
    currentTime: 0,
    playMode: PLAY_MODE.LOOP
};

const playerSlice = createSlice({
    name: 'player',
    initialState,
    reducers: {
        replaceSongList(state, action: PayloadAction<Song[]>) {
            state.playList = action.payload;
            state.currentSong = action.payload[0] || null;
        },
        setIsPlaying(state, action: PayloadAction<boolean>) {
            state.isPlaying = action.payload;
        },
        setCurrentTime(state, action: PayloadAction<number>) {
            state.currentTime = action.payload;
        },
        setCurrentSong(state, action: PayloadAction<Song>) {
            state.currentSong = action.payload;
        },
        setPlayMode(state, action: PayloadAction<PLAY_MODE>) {
            state.playMode = action.payload;
        }

    }
});

export const { replaceSongList ,setIsPlaying,setCurrentTime,setCurrentSong,setPlayMode} = playerSlice.actions;
export default playerSlice.reducer; 